/**
 * 
 */
package logic;

import editor.Grid;
import editor.Menu.OGGETTI;
import gameObjects.Bike.POINTTO;
import gameObjects.BikeCOM.TURNDIRECTION;

/**
 * @author Alessandro Gaeta
 *
 */
public class TurnController {
	
	private double 			time_with_same_direction;
	private OGGETTI[][]		cells = Grid.getGrid(); 
	private TURNDIRECTION 	next_step;
	private TargetFinder 	target_finder;
	private FollowPalyer 	followPlayer;
	
	
	
	public TurnController()
	{
		this.time_with_same_direction=0;
		this.target_finder=new TargetFinder();
		this.followPlayer=new FollowPalyer();
		
	}
	
	public TURNDIRECTION chooseDirection(POINTTO dir, float posX, float posY, boolean shield,boolean followPlayer)
	{
		next_step=checkWall(dir, posX, posY, shield);
		if(next_step==TURNDIRECTION.AWAY && !followPlayer)
		{
			next_step=target_finder.followTarget(dir,posX,posY);
			if(target_finder.getTarget().getX()==-1 && target_finder.getTarget().getX()==-1)
			{
				System.out.println("sto seguendo");
				next_step=this.followPlayer.follow(posX, posY, dir);
				System.out.println("AFTER: "+next_step);
			}
				//return next_step=this.randomTurn(dir, posX, posY);
		}
		return next_step;
	}
	
	@SuppressWarnings("unused")
	private TURNDIRECTION randomTurn(POINTTO dir, float posX, float posY)
	{
		time_with_same_direction=time_with_same_direction+0.02;
		int n = (int) (Math.random()*2); 
		double index_of_tourn =(Math.random()*50);
		if(index_of_tourn<time_with_same_direction){
			time_with_same_direction=0;
			switch(dir)
			{
			case NORTH:
				{ 
					if(n==0)
						if(cells[(int)posX+1][(int)posY]!=OGGETTI.WALL  && cells[(int)posX+1][(int)posY]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.LEFT;
						else 
							return TURNDIRECTION.AWAY;
					if(n==1)
						if(cells[(int)posX-1][(int)posY]!=OGGETTI.WALL  && cells[(int)posX-1][(int)posY]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.RIGHT;
						else
							return TURNDIRECTION.AWAY;
				}		
				break;
			case SOUTH:
				{
					if(n==0)
						if(cells[(int)posX-1][(int)posY]!=OGGETTI.WALL && cells[(int)posX-1][(int)posY]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.LEFT;
						else 
							return TURNDIRECTION.AWAY;
					if(n==1)
						if(cells[(int)posX+1][(int)posY]!=OGGETTI.WALL && cells[(int)posX+1][(int)posY]!=OGGETTI.EXTWALL)
							return  TURNDIRECTION.RIGHT;
						else
							return TURNDIRECTION.AWAY;
				}
				break;
			case EAST:
				{
					if(n==0)
						if(cells[(int)posX][(int)posY+1]!=OGGETTI.WALL && cells[(int)posX][(int)posY+1]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.LEFT;
						else 
							return TURNDIRECTION.AWAY;
					if(n==1)
						if(cells[(int)posX][(int)posY-1]!=OGGETTI.WALL && cells[(int)posX][(int)posY-1]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.RIGHT;
						else
							return TURNDIRECTION.AWAY;
				}
				break;
			case WEST:
				{
					if(n==0)
						if(cells[(int)posX][(int)posY-1]!=OGGETTI.WALL && cells[(int)posX][(int)posY-1]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.LEFT;
						else 
							return TURNDIRECTION.AWAY;
					if(n==1)
						if(cells[(int)posX][(int)posY+1]!=OGGETTI.WALL && cells[(int)posX][(int)posY+1]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.RIGHT;
						else
							return TURNDIRECTION.AWAY;
				}
				break;
			}
		}
		return TURNDIRECTION.AWAY;
	}
	
	
	
	private TURNDIRECTION checkWall(POINTTO dir, float posX, float posY,boolean shield)
	{
			int n = (int) (Math.random()*2); 
			//float prob_error = (float)(Math.random());
			//if(prob_error<1){
				System.out.println("SHIELD: "+shield);
				switch(dir) 
				{
				case NORTH:
					if((cells[(int)posX][(int)posY+1]==OGGETTI.WALL && !shield)
							||
							(cells[(int)posX][(int)posY+1]==OGGETTI.EXTWALL)
							||
							(cells[(int)posX][(int)posY+1]==OGGETTI.GATE)){
						if(n==0)
							if(cells[(int)posX+1][(int)posY]!=OGGETTI.WALL 
									&& 
									cells[(int)posX+1][(int)posY]!=OGGETTI.EXTWALL
									&&
									(cells[(int)posX+1][(int)posY]!=OGGETTI.GATE))
								return TURNDIRECTION.LEFT;
							else{
								if(cells[(int)posX-1][(int)posY]!=OGGETTI.WALL 
										&& 
										cells[(int)posX-1][(int)posY]!=OGGETTI.EXTWALL
										&&
										(cells[(int)posX-1][(int)posY]!=OGGETTI.GATE))
									return TURNDIRECTION.RIGHT;
								else 
									return TURNDIRECTION.AWAY;
							}
								
						if(n==1)
							if(cells[(int)posX-1][(int)posY]!=OGGETTI.WALL 
									&& 
									cells[(int)posX-1][(int)posY]!=OGGETTI.EXTWALL
									&&
									(cells[(int)posX-1][(int)posY]!=OGGETTI.GATE))
								return TURNDIRECTION.RIGHT;
							else{
								if(cells[(int)posX+1][(int)posY]!=OGGETTI.WALL 
										&& 
										cells[(int)posX+1][(int)posY]!=OGGETTI.EXTWALL
										&&
										(cells[(int)posX+1][(int)posY]!=OGGETTI.GATE))
									return TURNDIRECTION.LEFT;
								else 
									return TURNDIRECTION.AWAY;
							}
					}
								
					break;
				case SOUTH:
					if((cells[(int)posX][(int)posY-1]==OGGETTI.WALL && !shield)
							||
							(cells[(int)posX][(int)posY-1]==OGGETTI.EXTWALL)
							||
							(cells[(int)posX][(int)posY-1]==OGGETTI.GATE)){
						if(n==0)
							if(cells[(int)posX-1][(int)posY]!=OGGETTI.WALL 
									&&
									cells[(int)posX-1][(int)posY]!=OGGETTI.EXTWALL
									&&
									(cells[(int)posX-1][(int)posY]!=OGGETTI.GATE))
								return TURNDIRECTION.LEFT;
							else 
							{
								if(cells[(int)posX+1][(int)posY]!=OGGETTI.WALL 
										&& 
										cells[(int)posX+1][(int)posY]!=OGGETTI.EXTWALL
										&&
										(cells[(int)posX+1][(int)posY]!=OGGETTI.GATE))
									return TURNDIRECTION.RIGHT;
								else 
									return TURNDIRECTION.AWAY;
							}

						if(n==1)
							if(cells[(int)posX+1][(int)posY]!=OGGETTI.WALL 
									&& 
									cells[(int)posX+1][(int)posY]!=OGGETTI.EXTWALL
									&&
									(cells[(int)posX+1][(int)posY]!=OGGETTI.GATE))
								return  TURNDIRECTION.RIGHT;
							else{
								if(cells[(int)posX-1][(int)posY]!=OGGETTI.WALL 
										&&
										cells[(int)posX-1][(int)posY]!=OGGETTI.EXTWALL
										&&
										(cells[(int)posX-1][(int)posY]!=OGGETTI.GATE))
									return TURNDIRECTION.LEFT;
								else 
									return TURNDIRECTION.AWAY;
							}
					}
					break;
				case EAST:
					if((cells[(int)posX-1][(int)posY]==OGGETTI.WALL&& !shield)
							||
							(cells[(int)posX-1][(int)posY]==OGGETTI.EXTWALL)
							||
							(cells[(int)posX-1][(int)posY]==OGGETTI.GATE)){
						if(n==0)
							if(cells[(int)posX][(int)posY+1]!=OGGETTI.WALL 
									&& 
									cells[(int)posX][(int)posY+1]!=OGGETTI.EXTWALL
									&&
									(cells[(int)posX][(int)posY+1]!=OGGETTI.GATE))
								return TURNDIRECTION.LEFT;
							else{
								if(cells[(int)posX][(int)posY-1]!=OGGETTI.WALL 
										&& 
										cells[(int)posX][(int)posY-1]!=OGGETTI.EXTWALL
										&&
										(cells[(int)posX][(int)posY-1]!=OGGETTI.GATE))
									return TURNDIRECTION.RIGHT;
								else 
									return TURNDIRECTION.AWAY;
							}
						if(n==1)
							if(cells[(int)posX][(int)posY-1]!=OGGETTI.WALL 
									&& 
									cells[(int)posX][(int)posY-1]!=OGGETTI.EXTWALL
									&&
									(cells[(int)posX][(int)posY-1]!=OGGETTI.GATE))
								return TURNDIRECTION.RIGHT;
							else{
								if(cells[(int)posX][(int)posY+1]!=OGGETTI.WALL 
										&& 
										cells[(int)posX][(int)posY+1]!=OGGETTI.EXTWALL
										&&
										(cells[(int)posX][(int)posY+1]!=OGGETTI.GATE))
									return TURNDIRECTION.LEFT;
								else 
									return TURNDIRECTION.AWAY;
							}
								
					}
					break;
				case WEST:
					if((cells[(int)posX+1][(int)posY]==OGGETTI.WALL && !shield)
							||
							(cells[(int)posX+1][(int)posY]==OGGETTI.EXTWALL)
							||
							(cells[(int)posX+1][(int)posY]==OGGETTI.GATE)){
						if(n==0)
							if(cells[(int)posX][(int)posY-1]!=OGGETTI.WALL 
									&& 
									cells[(int)posX][(int)posY-1]!=OGGETTI.EXTWALL
									&&
									(cells[(int)posX][(int)posY-1]!=OGGETTI.GATE))
								return TURNDIRECTION.LEFT;
							else {
								if(cells[(int)posX][(int)posY+1]!=OGGETTI.WALL 
										&& 
										cells[(int)posX][(int)posY+1]!=OGGETTI.EXTWALL
										&&
										(cells[(int)posX][(int)posY+1]!=OGGETTI.GATE))
									return TURNDIRECTION.RIGHT;
								else 
									return TURNDIRECTION.AWAY;
							}
						if(n==1)
							if(cells[(int)posX][(int)posY+1]!=OGGETTI.WALL 
									&& 
									cells[(int)posX][(int)posY+1]!=OGGETTI.EXTWALL
									&&
									(cells[(int)posX][(int)posY+1]!=OGGETTI.GATE))
								return TURNDIRECTION.RIGHT;
							else
							{
								if(cells[(int)posX][(int)posY-1]!=OGGETTI.WALL 
										&& 
										cells[(int)posX][(int)posY-1]!=OGGETTI.EXTWALL
										&&
										(cells[(int)posX][(int)posY-1]!=OGGETTI.GATE))
									return TURNDIRECTION.LEFT;
								else 
									return TURNDIRECTION.AWAY;
							}
					}
					break;
				}
			//}
			return TURNDIRECTION.AWAY;
	}
	
}
